package com.ymatou.app.rec.testcase;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.app.rec.parameter.GlProductBean;
import com.ymatou.app.rec.service.GlProductCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;



public class Ts_GlProduct {
	private static GlProductBean glproductBean;
	private static GlProductCall glproductCall;
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("聚洋货猜你喜欢推荐接口");
	}

	@Before
	public void caseUp() {
		glproductBean = new GlProductBean();
		glproductCall = new GlProductCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(glproductCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GlProduct_001() {
		Logger.start(true,"特卖首页底部猜你喜欢商品推荐-验证接口返回公共数据 ");
		try {
			//设置参数
			String requestid = "1111111";
			String module = "jyh_sale_gl_pro";
			String submodule= "pdt_list_md";
			String platform = "appbuyer";
			String sort = "algo1";
			
			glproductBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,sort:"+sort+"");
			glproductBean.setAppkey("1");
			glproductBean.setRequestid(requestid);
			glproductBean.setModule(module);
			glproductBean.setSubmodule(submodule);
			glproductBean.setPlatform(platform);
			glproductBean.setLimit("0,10");
			
			glproductCall.setData(glproductBean);
			glproductCall.callService();
			
			JSONArray hits = glproductCall.getHits();
			
			Logger.verifyEquals("OK",glproductCall.getCommonInfo("status") , "检查接口状态值");
			Logger.verifyNotNull(glproductCall.getCommonInfo("took"), "took不为空");
			Logger.verifyEquals(false, glproductCall.getCommonInfoBoo("downgrade"), "验证downgrade返回false");
			Logger.verifyEquals(false, glproductCall.getCommonInfoBoo("timeout"), "验证timeout返回flase");
			Logger.verifyEquals(requestid, glproductCall.getCommonInfo("requestid"), "验证requestid是否为所传值");
			Logger.verifyNotNull(glproductCall.getCommonInfo("hostip"), "hostip不为空");
			Logger.verifyEquals(sort, glproductCall.getCommonInfo("algorithm"), "验证返回排序算法是否为所传方法");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GlProduct_002() {
		Logger.start(true,"新首页底部猜你喜欢商品推荐-验证接口返回公共数据 ");
		try {
			//设置参数
			String requestid = "1111111";
			String module = "homepage_gl_pro";
			String submodule= "pdt_list_md";
			String platform = "appbuyer";
			String sort = "algo1";
			
			glproductBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,sort:"+sort+"");
			glproductBean.setAppkey("1");
			glproductBean.setRequestid(requestid);
			glproductBean.setModule(module);
			glproductBean.setSubmodule(submodule);
			glproductBean.setPlatform(platform);
			glproductBean.setLimit("0,10");
			
			glproductCall.setData(glproductBean);
			glproductCall.callService();
			
			JSONArray hits = glproductCall.getHits();
			
			Logger.verifyEquals("OK",glproductCall.getCommonInfo("status") , "检查接口状态值");
			Logger.verifyNotNull(glproductCall.getCommonInfo("took"), "took不为空");
			Logger.verifyEquals(false, glproductCall.getCommonInfoBoo("downgrade"), "验证downgrade返回false");
			Logger.verifyEquals(false, glproductCall.getCommonInfoBoo("timeout"), "验证timeout返回flase");
			Logger.verifyEquals(requestid, glproductCall.getCommonInfo("requestid"), "验证requestid是否为所传值");
			Logger.verifyNotNull(glproductCall.getCommonInfo("hostip"), "hostip不为空");
			Logger.verifyEquals(sort, glproductCall.getCommonInfo("algorithm"), "验证返回排序算法是否为所传方法");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GlProduct_003() {
		Logger.start(true,"新用户猜你喜欢-验证接口返回公共数据 ");
		try {
			//设置参数
			String requestid = "1111111";
			String module = "new_user_gl_pro";
			String submodule= "pdt_list_md";
			String platform = "appbuyer";
			String sort = "algo1";
			
			glproductBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,sort:"+sort+"");
			glproductBean.setAppkey("1");
			glproductBean.setRequestid(requestid);
			glproductBean.setModule(module);
			glproductBean.setSubmodule(submodule);
			glproductBean.setPlatform(platform);
			glproductBean.setLimit("0,10");
			
			glproductCall.setData(glproductBean);
			glproductCall.callService();
			
			JSONArray hits = glproductCall.getHits();
			
			Logger.verifyEquals("OK",glproductCall.getCommonInfo("status") , "检查接口状态值");
			Logger.verifyNotNull(glproductCall.getCommonInfo("took"), "took不为空");
			Logger.verifyEquals(false, glproductCall.getCommonInfoBoo("downgrade"), "验证downgrade返回false");
			Logger.verifyEquals(false, glproductCall.getCommonInfoBoo("timeout"), "验证timeout返回flase");
			Logger.verifyEquals(requestid, glproductCall.getCommonInfo("requestid"), "验证requestid是否为所传值");
			Logger.verifyNotNull(glproductCall.getCommonInfo("hostip"), "hostip不为空");
			Logger.verifyEquals(sort, glproductCall.getCommonInfo("algorithm"), "验证返回排序算法是否为所传方法");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GlProduct_004() {
		Logger.start(true, "特卖首页底部猜你喜欢商品推荐-检查接口返回数据，确保不会重复 ");
		try {
			// 设置参数
			String requestid = "1111111";
			String module = "jyh_sale_gl_pro";
			String submodule = "pdt_list_md";
			String platform = "appbuyer";
			String sort = "algo1";

			glproductBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,sort:"
					+ sort + "");
			glproductBean.setAppkey("1");
			glproductBean.setRequestid(requestid);
			glproductBean.setModule(module);
			glproductBean.setSubmodule(submodule);
			glproductBean.setPlatform(platform);
			glproductBean.setLimit("0,10");

			glproductCall.setData(glproductBean);
			glproductCall.callService();

			Logger.verifyEquals("OK", glproductCall.getStatus(), "验证接口返回状态是否正常");
			JSONArray hitsAry = glproductCall.getHits();

			List<String> list = new ArrayList<String>();

			for (int i = 1; i <= hitsAry.length(); i++) {

				String ids = hitsAry.get(i - 1).toString();
				JSONObject json = new JSONObject(ids);

				String sproductid = json.getString("sproductid");

				if (list.contains(sproductid) == true) {

					Logger.verifyEquals(false, list.contains(sproductid), "验证第"+ i + "条返回数据的id存在重复值" + sproductid);

				} else {
					Logger.verifyEquals(false, list.contains(sproductid), "验证第"+ i + "条返回数据的id不存在重复值" + sproductid);

					list.add(sproductid);

				}
			}

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GlProduct_005() {
		Logger.start(true,"新首页底部猜你喜欢商品推荐-检查接口返回数据，确保不会重复 ");
		try {
			//设置参数
			String requestid = "1111111";
			String module = "homepage_gl_pro";
			String submodule= "pdt_list_md";
			String platform = "appbuyer";
			String sort = "algo1";
			
			glproductBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,sort:"+sort+"");
			glproductBean.setAppkey("1");
			glproductBean.setRequestid(requestid);
			glproductBean.setModule(module);
			glproductBean.setSubmodule(submodule);
			glproductBean.setPlatform(platform);
			glproductBean.setLimit("0,10");
			
			glproductCall.setData(glproductBean);
			glproductCall.callService();
			
			Logger.verifyEquals("OK", glproductCall.getStatus(), "验证接口返回状态是否正常");
			JSONArray hitsAry = glproductCall.getHits();

			List<String> list = new ArrayList<String>();

			for (int i = 1; i <= hitsAry.length(); i++) {

				String ids = hitsAry.get(i - 1).toString();
				JSONObject json = new JSONObject(ids);

				String sproductid = json.getString("sproductid");

				if (list.contains(sproductid) == true) {

					Logger.verifyEquals(false, list.contains(sproductid), "验证第"+ i + "条返回数据的id存在重复值" + sproductid);

				} else {
					Logger.verifyEquals(false, list.contains(sproductid), "验证第"+ i + "条返回数据的id不存在重复值" + sproductid);

					list.add(sproductid);

				}
			}
		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GlProduct_006() {
		Logger.start(true,"新用户猜你喜欢-检查接口返回数据，确保不会重复 ");
		try {
			//设置参数
			String requestid = "1111111";
			String module = "new_user_gl_pro";
			String submodule= "pdt_list_md";
			String platform = "appbuyer";
			String sort = "algo1";
			
			glproductBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,sort:"+sort+"");
			glproductBean.setAppkey("1");
			glproductBean.setRequestid(requestid);
			glproductBean.setModule(module);
			glproductBean.setSubmodule(submodule);
			glproductBean.setPlatform(platform);
			glproductBean.setLimit("0,10");
			
			glproductCall.setData(glproductBean);
			glproductCall.callService();
			
			Logger.verifyEquals("OK", glproductCall.getStatus(), "验证接口返回状态是否正常");
			JSONArray hitsAry = glproductCall.getHits();

			List<String> list = new ArrayList<String>();

			for (int i = 1; i <= hitsAry.length(); i++) {

				String ids = hitsAry.get(i - 1).toString();
				JSONObject json = new JSONObject(ids);

				String sproductid = json.getString("sproductid");

				if (list.contains(sproductid) == true) {

					Logger.verifyEquals(false, list.contains(sproductid), "验证第"+ i + "条返回数据的id存在重复值" + sproductid);

				} else {
					Logger.verifyEquals(false, list.contains(sproductid), "验证第"+ i + "条返回数据的id不存在重复值" + sproductid);

					list.add(sproductid);

				}
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GlProduct_007() {
		Logger.start(true,"特卖首页底部猜你喜欢商品推荐-验证searchInfo里的返回数据 ");
		try {
			//设置参数
			String requestid = "1111111";
			String module = "jyh_sale_gl_pro";
			String submodule= "pdt_list_md";
			String platform = "appbuyer";
			String sort = "algo1";
			
			glproductBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,sort:"+sort+"");
			glproductBean.setAppkey("1");
			glproductBean.setRequestid(requestid);
			glproductBean.setModule(module);
			glproductBean.setSubmodule(submodule);
			glproductBean.setPlatform(platform);
			glproductBean.setLimit("0,10");
			
			glproductCall.setData(glproductBean);
			glproductCall.callService();
			
			JSONObject searchInfoJsonObject= (JSONObject) glproductCall.getSearchInfo();
			Logger.verifyEquals(true, searchInfoJsonObject.has("module"), "验证searchInfo里是否有返回module模块字段信息");
			Logger.verifyEquals(true, searchInfoJsonObject.has("algo"), "验证searchInfo里是否有返回algo算法字段信息");
			Logger.verifyEquals(true, searchInfoJsonObject.has("yid"), "验证searchInfo里是否有返回yid字段信息");
			Logger.verifyEquals(module, searchInfoJsonObject.get("module"), "验证返回module值");
			Logger.verifyEquals(sort, searchInfoJsonObject.get("algo"), "验证返回algo值");
			Logger.verifyNotNull(searchInfoJsonObject.get("yid"), "验证返回yid不能为空");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GlProduct_008() {
		Logger.start(true,"新首页底部猜你喜欢商品推荐-验证searchInfo里的返回数据 ");
		try {
			//设置参数
			String requestid = "1111111";
			String module = "homepage_gl_pro";
			String submodule= "pdt_list_md";
			String platform = "appbuyer";
			String sort = "algo1";
			
			glproductBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,sort:"+sort+"");
			glproductBean.setAppkey("1");
			glproductBean.setRequestid(requestid);
			glproductBean.setModule(module);
			glproductBean.setSubmodule(submodule);
			glproductBean.setPlatform(platform);
			glproductBean.setLimit("0,10");
			
			glproductCall.setData(glproductBean);
			glproductCall.callService();
			
			JSONObject searchInfoJsonObject= (JSONObject) glproductCall.getSearchInfo();
			Logger.verifyEquals(true, searchInfoJsonObject.has("module"), "验证searchInfo里是否有返回module模块字段信息");
			Logger.verifyEquals(true, searchInfoJsonObject.has("algo"), "验证searchInfo里是否有返回algo算法字段信息");
			Logger.verifyEquals(true, searchInfoJsonObject.has("yid"), "验证searchInfo里是否有返回yid字段信息");
			Logger.verifyEquals(module, searchInfoJsonObject.get("module"), "验证返回module值");
			Logger.verifyEquals(sort, searchInfoJsonObject.get("algo"), "验证返回algo值");
			Logger.verifyNotNull(searchInfoJsonObject.get("yid"), "验证返回yid不能为空");
			Logger.verifyNotNull(searchInfoJsonObject.get("nextupdatetime"), "nextupdatetime更新时间不为空");
			Logger.verifyNotNull(searchInfoJsonObject.get("currtupdatetime"), "currtupdatetime更新时间不为空");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GlProduct_009() {
		Logger.start(true,"新用户猜你喜欢-验证searchInfo里的返回数据");
		try {
			//设置参数
			String requestid = "1111111";
			String module = "new_user_gl_pro";
			String submodule= "pdt_list_md";
			String platform = "appbuyer";
			String sort = "algo1";
			
			glproductBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,sort:"+sort+"");
			glproductBean.setAppkey("1");
			glproductBean.setRequestid(requestid);
			glproductBean.setModule(module);
			glproductBean.setSubmodule(submodule);
			glproductBean.setPlatform(platform);
			glproductBean.setLimit("0,10");
			
			glproductCall.setData(glproductBean);
			glproductCall.callService();
			
			JSONObject searchInfoJsonObject= (JSONObject) glproductCall.getSearchInfo();
			Logger.verifyEquals(true, searchInfoJsonObject.has("module"), "验证searchInfo里是否有返回module模块字段信息");
			Logger.verifyEquals(true, searchInfoJsonObject.has("algo"), "验证searchInfo里是否有返回algo算法字段信息");
			Logger.verifyEquals(true, searchInfoJsonObject.has("yid"), "验证searchInfo里是否有返回yid字段信息");
			Logger.verifyEquals(module, searchInfoJsonObject.get("module"), "验证返回module值");
			Logger.verifyEquals(sort, searchInfoJsonObject.get("algo"), "验证返回algo值");
			Logger.verifyNotNull(searchInfoJsonObject.get("yid"), "验证返回yid不能为空");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GlProduct_010() {
		Logger.start(true,"特卖首页底部猜你喜欢商品推荐-验证接口返回总记录数 ");
		try {
			//设置参数
			String requestid = "1111111";
			String module = "jyh_sale_gl_pro";
			String submodule= "pdt_list_md";
			String platform = "appbuyer";
			String sort = "algo1";
			int limit = 3;
			
			glproductBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,sort:"+sort+"");
			glproductBean.setAppkey("1");
			glproductBean.setRequestid(requestid);
			glproductBean.setModule(module);
			glproductBean.setSubmodule(submodule);
			glproductBean.setPlatform(platform);
			glproductBean.setLimit("0,"+limit);
			
			glproductCall.setData(glproductBean);
			glproductCall.callService();
			
			JSONArray hits = glproductCall.getHits();
			int total = glproductCall.getTotal();
			
			Logger.verifyEquals(true,total>limit, "检查总记录数是大于当前页总数的");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GlProduct_011() {
		Logger.start(true,"新首页底部猜你喜欢商品推荐-验证接口返回总记录数 ");
		try {
			//设置参数
			String requestid = "1111111";
			String module = "homepage_gl_pro";
			String submodule= "pdt_list_md";
			String platform = "appbuyer";
			String sort = "algo1";
			int limit = 3;
			
			glproductBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,sort:"+sort+"");
			glproductBean.setAppkey("1");
			glproductBean.setRequestid(requestid);
			glproductBean.setModule(module);
			glproductBean.setSubmodule(submodule);
			glproductBean.setPlatform(platform);
			glproductBean.setLimit("0,"+limit+"");
			
			glproductCall.setData(glproductBean);
			glproductCall.callService();
			
			JSONArray hits = glproductCall.getHits();
			int total = glproductCall.getTotal();
			
			Logger.verifyEquals(true,total>limit, "检查总记录数是大于当前页总数的");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GlProduct_012() {
		Logger.start(true,"新用户猜你喜欢-验证接口返回总记录数 ");
		try {
			//设置参数
			String requestid = "1111111";
			String module = "new_user_gl_pro";
			String submodule= "pdt_list_md";
			String platform = "appbuyer";
			String sort = "algo1";
			int limit = 3;
			
			glproductBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,sort:"+sort+"");
			glproductBean.setAppkey("1");
			glproductBean.setRequestid(requestid);
			glproductBean.setModule(module);
			glproductBean.setSubmodule(submodule);
			glproductBean.setPlatform(platform);
			glproductBean.setLimit("0,"+limit+"");
			
			glproductCall.setData(glproductBean);
			glproductCall.callService();
			
			JSONArray hits = glproductCall.getHits();
			int total = glproductCall.getTotal();
			
			Logger.verifyEquals(true,total>limit, "检查总记录数是大于当前页总数的");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
